package com.softmaker;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileToBinary {

    public String  get() throws IOException {
        String filePath = "E:\\file\\c35daea9-9e66-4048-92f6-f5fc93f32c33.mp4"; // 替换为你的文件路径


            // 1. 创建文件对象
            File file = new File(filePath);

            // 2. 创建 FileInputStream 对象以读取文件
            FileInputStream fis = new FileInputStream(file);

            // 3. 创建一个字节数组，用于存储文件内容
            byte[] byteArray = new byte[(int) file.length()];

            // 4. 读取文件内容到字节数组
            fis.read(byteArray);

            // 5. 关闭 FileInputStream
            fis.close();

            // 6. 将字节数组转换为二进制字符串
            String binaryString = bytesToBinaryString(byteArray);

            // 7. 打印二进制字符串
            System.out.println("Binary representation of the file content:\n" + binaryString);


        return binaryString;
    }
    private static String bytesToBinaryString(byte[] bytes) {
        StringBuilder binaryStringBuilder = new StringBuilder();
        for (byte b : bytes) {
            // 将每个字节转换为8位的二进制字符串
            String binary = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
            binaryStringBuilder.append(binary);
        }
        return binaryStringBuilder.toString();
    }
}